<?php include $this->admin_tpl('meta');?>
<title>会员管理</title>
<meta name="referrer" content="no-referrer" />
<style>
.yzm-tips-icon{font-weight:normal;}
.label{margin-left:3px;height:20px;line-height:20px;}
.table-hover tbody tr:hover td,.table-hover tbody tr:hover th{background-color:#f2dede;color:#b94a48;}
</style>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 聚水潭 <span class="c-gray en">&gt;</span> 佑晟订做 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
  <div> 
    <form action="<?php echo $url;?>" method="get" id="search">
	 <input type="hidden" value="<?php echo ROUTE_M;?>" name="m">
	 <input type="hidden" value="<?php echo ROUTE_C;?>" name="c">
	 <input type="hidden" value="0" name="download">
	 <input type="hidden" value="1" name="dosubmit">
	 <input type="hidden" value="<? if(isset($_GET['of'])) echo $_GET['of']?>" name="of">
	 <input type="hidden" value="<? if(isset($_GET['or'])) echo $_GET['or']?>" name="or">
	 <input type="hidden" value="0" name="download">
	<input type="text" name="other_2" class="input-text" style="width:170px" placeholder="其他属性2" value="<?php if(isset($_GET['other_2'])) echo $_GET['other_2'];?>">
	<input type="text" name="sku_name" class="input-text" style="width:170px" placeholder="商品名称" value="<?php if(isset($_GET['sku_name'])) echo $_GET['sku_name'];?>">
     <span class="select-box inline">
		<select name="buhuo_by_days" class="select">
			<option value="2" <?php if($buhuo_by_days == '2'){ echo 'selected'; } ?>>按2天销量统计补货</option>
			<option value="3" <?php if($buhuo_by_days == '3'){ echo 'selected'; } ?>>按3天销量统计补货</option>
			<option value="7" <?php if($buhuo_by_days == '7'){ echo 'selected'; } ?>>按7天销量统计补货</option>
			<option value="30" <?php if($buhuo_by_days == '30'){ echo 'selected'; } ?>>按30天销量统计补货</option>
		</select>
	</span>
	<button name="dosubmit" class="btn btn-success" type="submit" value="1" id="btn_search"><i class="Hui-iconfont">&#xe665;</i> 搜索</button>
	<a class="btn" href="<?php echo $url;?>"><i class="Hui-iconfont">&#xe66b;</i> 清空</a>
	<button name="dosubmit" class="btn btn-warning" type="button" id="btn_download"><i class="Hui-iconfont">&#xe640;</i> 导出搜索结果</button>
	 </form>	
  </div>
  <div class="cl pd-5 mt-20 bg-1 bk-gray" id="top_nav"> 
  <?php if(!$memberinfo){?>
  <span class="l"> <a href="javascript:;" class="btn btn-success radius" id="btn_sales_by_sku"><i class="Hui-iconfont">&#xe68f;</i> 重新统计</a></span>
  <?php }?>
  <span class="r" id="total"></span></div>
  <form method="post" action="<?php echo U('del');?>" id="myform">
  <table class="table table-border table-bordered table-bg table-hover yzm-table-sort" id="top" style="z-index:100;">
    <thead>
      <tr class="text-c">
		<th width="50">编号</th>
		<th width="80">款号</th>
		<th width="">商品名称</th>
		<th width="120">图片/销售品牌</th>
		<th width="80">昨天销量<?php echo field_order('sum_1days');?></th>
		<th width="80">2天销量<?php echo field_order('sum_2days');?></th>
		<th width="80">3天销量<?php echo field_order('sum_3days');?></th>
		<th width="80">7天销量<?php echo field_order('sum_7days');?></th>
		<th width="80">30天销量<?php echo field_order('sum_30days');?></th>
		<th width="80">趋势 <span class="yzm-explain-box"><span class="yzm-explain">（3天日均-7天日均）/7天日均，浮动超20%，否则持平</span></span></th>
		<th width="100">库存<?php echo field_order('total_qty');?></th>
		<th width="100">生产中<?php echo field_order('purchase_total_qty');?><span class="yzm-explain-box"><span class="yzm-explain">佑晟共享仓所创建订做下单的采购单</span></span></th>
		<th width="100">建议补货<?php echo field_order('buhuo_total_qty');?><span class="yzm-explain-box"><span class="yzm-explain">建议补货数=选择的天数销量/天数*（生产周期+1天查货时间）-生产中库存-仓库库存</span></span></th>
		<th width="100">补货周期（天）</th>
		<th width="100">销量统计时间<span class="yzm-explain-box"><span class="yzm-explain">每4小时统计一次</span></span></th>
		<!--<th width="100">操作</th>-->
      </tr>
    </thead>
	</table>
	<div style="display:none;height:40px;" id="box"></div>
	
	
<div style="position:relative;clear:both" id="my">
  <table class="table table-border table-bordered table-bg table-hover yzm-table-sort" id="table">
    <tbody>
		<?php 
		$i=1;
		if(isset($list)){
		$total =$sum_1days=$sum_2days=$sum_3days=$sum_7days=$sum_30days=$purchase_total_qty=$buhuo_total_qty= 0;
		foreach($list as $key=>$val){
			$total += $val['total_qty'];
			$sum_1days += $val['sum_1days'];$sum_2days += $val['sum_2days'];$sum_3days += $val['sum_3days'];$sum_7days += $val['sum_7days'];$sum_30days += $val['sum_30days'];
			$purchase_total_qty += $val['purchase_total_qty'];
			$buhuo_total_qty += $val['buhuo_total_qty'];
			if(strpos($val['pic'],'://')){
				$val['pic'] = 'https://'.explode('://',$val['pic'])[1];
			}
		?>
		  <tr class="text-c item" child="child_<?php echo $i;?>">
			<td width="50"><?php echo $i;$i++;?></td>
			<td width="80"><?php echo $key;?></td>
			<td width=""><?php echo $val['name'];?></td>
			<td width="120">
				<a title="点击预览" href="javascript:void(0);" class="show_img" onclick="yzm_show_img('<?php echo $val['pic']?>',600,600);" style="text-decoration:none">
					<img src="<?php echo $val['pic']?>" height="40" style="max-width:100px" onmouseover="yzm_img_browse(this, '<?php echo $val['pic']?>')" onmouseout="layer.closeAll('tips')">
				</a>
			</td>
			<td width="80"><?php echo $val['sum_1days'];?></td>
			<td width="80"><?php echo $val['sum_2days'];?></td>
			<td width="80"><?php echo $val['sum_3days'];?></td>
			<td width="80"><?php echo $val['sum_7days'];?></td>
			<td width="80"><?php echo $val['sum_30days'];?></td>
			<td width="80">
			<?php 
			$val['trend'] = avg_trend($val['sum_3days']/3,$val['sum_7days']/7);
			if($val['trend']==0){
				$class = '';
				$text = '持平';
				$font = 'xe6a1';
			}else if($val['trend']>0){
				$class = 'c-red';
				$text = '上升'.$val['trend'].'%';
				$font = 'xe679';
			}else if($val['trend']<0){
				$class = 'c-green';
				$text = '下降'.($val['trend']*-1).'%';
				$font = 'xe674';
			} ?>
			 <span><a onmouseover="yzm_tips=layer.tips('<?php echo $text;?>，点击查看详情', this)" onmouseout="layer.close(yzm_tips);" href="javascript:;" class="other_2" other_2="<?php echo $key;?>" title="查看统计"><span class="label label-default radius" style="width:16px;text-align:center;font-size:16px;;"><i class="Hui-iconfont <?php echo $class;?>">&#<?php echo $font;?></i></span></span>
			</td>
			<td width="100"><?php echo $val['total_qty'];?></td>
			<td width="100">
			<?php echo $val['purchase_total_qty'];?>
			<?php if($val['purchase_total_qty']>0){?>
<span><a onmouseover="yzm_tips=layer.tips('查看采购单', this)" onmouseout="layer.close(yzm_tips);" href="javascript:;" class="purchaser" other_2="<?php echo $key;?>" title="查看采购单"><span class="label label-default radius" style="width:16px;text-align:center;font-size:16px;;"><i class="Hui-iconfont">&#xe627</i></span>
			<?php } ?>

			</td>
			<td width="100"><?php echo $val['buhuo_total_qty'];?></td>
			<td width="100"><?php echo $val['cycel'];?></td>
			<td width="100"><?php if(isset($val['sales_add_time'])&&$val['sales_add_time']){echo date("y-m-d H:i",$val['sales_add_time']);}else{echo '-';}?></td>
		  </tr>
		  <?php foreach($val['chima'] as $k=>$v){ 
			if(isset($v['brand_list'])&&$v['brand_list']){
				$count=count($v['brand_list']);
				foreach($v['brand_list'] as $item=>$data){ ?>
		  <tr class="text-c child_<?php echo $i-1;?>" style="display: none;background:#f5fafe;">
		    <?php if(isset($count)){?><td rowspan="<?php echo $count;?>">└─</td><?php } ?>
			<?php if(isset($count)){?> <td colspan="2" rowspan="<?php echo $count;?>"><?php echo $v['other_2'];?></td><?php } ?>
			<td><?php echo $item;?></td>
			<td><?php echo $data['sum_1days'];?></td>
			<td><?php echo $data['sum_2days'];?></td>
			<td><?php echo $data['sum_3days'];?></td>
			<td><?php echo $data['sum_7days'];?></td>
			<td><?php echo $data['sum_30days'];?></td>
			<?php if(isset($count)){?> 
			<td rowspan="<?php echo $count;?>">-</td>
			<td rowspan="<?php echo $count;?>"><?php echo $v['qty'];?></td>
			<td rowspan="<?php echo $count;?>"><?php echo $v['purchase_qty'];?></td>
			<td rowspan="<?php echo $count;?>"><?php echo $v['buhuo_qty'];?></td>
			<td rowspan="<?php echo $count;?>"><?php echo $val['cycel'];?></td>
			<td rowspan="<?php echo $count; unset($count);?>"><?php if(isset($other_2_sum[$v['other_2']]['sales_add_time'])&&$other_2_sum[$v['other_2']]['sales_add_time']){echo date("y-m-d H:i",$other_2_sum[$v['other_2']]['sales_add_time']);}else{echo '-';}?></td>
			<?php } ?>
		  </tr>
			<?php }
		  }else{
		  
		  ?>
		  <tr class="text-c child_<?php echo $i-1;?>" style="display: none;background:#f5fafe;">
		  <td>└─</td>
		  <td colspan="2"><?php echo $k;?></td>
		  <td colspan="7">无销量数据/未上线</td>
		  <td><?php echo $v['qty'];?></td>
		  <td><?php echo $v['purchase_qty'];?></td>
		  <td><?php echo $v['buhuo_qty'];?></td>
		  <td><?php echo $val['cycel'];?></td>
		  <td>-</td>
		  </tr>
		  <?php }?>
		  <?php }?>
		  
		<?php 
		}
		}
		?>
		  <tr class="text-c">
			<td colspan="4"><strong>总计</strong></td>
			<td><strong><?php echo $sum_1days;?></strong></td>
			<td><strong><?php echo $sum_2days;?></strong></td>
			<td><strong><?php echo $sum_3days;?></strong></td>
			<td><strong><?php echo $sum_7days;?></strong></td>
			<td><strong><?php echo $sum_30days;?></strong></td>
			<td><strong></strong></td>
			<td><strong><?php echo $total;?></strong></td>
			<td><strong><?php echo $purchase_total_qty;?></strong></td>
			<td><strong><?php echo $buhuo_total_qty;?></strong></td>
			<td></td>
			<td></td>
		  </tr>
    </tbody>
  </table>
  </div>
  </form>
   <?php if(!isset($list)||!$list){ ?><div class="yzm_no_data"><i class="Hui-iconfont">&#xe6e0;</i> 暂无数据</div><?php } ?>
</div>
<?php include $this->admin_tpl('footer', 'admin');?>
<script type="text/javascript" src="<?php echo STATIC_URL;?>plugin/laydate/1.1/laydate.js"></script>
<script>
$(function(){
	$('#total').html('<strong>共<?php echo $i; ?>款，库存：<?php echo $total;?>件<strong>');
	var width = $('#table').width();
	//判断是否有滚动条
	$('#top').css('width',(width)+'px');
	document.addEventListener('scroll', function () {
		var top = $(document).scrollTop();
		if(top>110){
			$('#top').css('top','0');
			$('#top').css('position','fixed');
			$('#box').css('display','');
		}else{
			$('#top').css('position','');
			$('#box').css('display','none');
		}
	})

	$('.item').click(function(){
		var child = $(this).attr('child');
		$('.item').css('background','');
		if($('.'+child).is(":hidden")){
			$(this).css('background','#dff0d8');
			$('.'+child).show();
		}else{
			$('.'+child).hide();
		}
		var width = $('#table').width();
		//判断是否有滚动条
		$('#top').css('width',(width)+'px');
	})
	
	$('#btn_download').click(function(){
		$('input[name="download"]').val(1);
		//方法调用
		var t = $('form').serializeArray();
		var d = '<?php echo $url;?>?';
		$.each(t, function(i,v) {
			d +=v['name']+'='+v['value']+'&';
		});
		openNewWindow(d);
		$('input[name="download"]').val(0);
	})
	$('#btn_search').click(function(){
		$('input[name="download"]').val(0);
		$('#search').submit();
	})
	$('#btn_sales_by_sku').click(function(){
		sales_by_sku(0)
	})
	
	$('.other_2').on('click',function(){
		let width = '800px';
		if(screen.width<800){
			width = '100%';
		}
		let other_2 = $(this).attr('other_2');
		let url = '<?php echo U('trend',['type'=>'other_2'])?>'+'?kuan='+other_2;
		layer.open({  
			type: 2,  
			title: other_2+'销量统计',  
			shadeClose: true,  
			shade: 0.2,  
			area: [width, '600px'],  
			content: url,
			success: function(layero, index){
				
			}
		})
		return false;
	})
	$('.show_img').click(function(){
		return false;
	})
	
	$('.purchaser').on('click',function(){
		let width = (screen.width-500)+'px';
		let height = (screen.height-300)+'px';
		if(screen.width<1200){
			width = '100%';
		}
		let other_2 = $(this).attr('other_2');
		let url = '<?php echo U('purchaser');?>'+'?other_2='+other_2+'&status=Confirmed&dosubmit=1&company=<?php echo $company;?>';
		//yzm_open_full(other_2+'采购单',url);
		//return false;
		layer.open({  
			type: 2,  
			title: other_2+'采购单',  
			shadeClose: true,  
			shade: 0.2,  
			area: [width, height],  
			content: url,
			success: function(layero, index){
				
			}
		})
		return false;
	})
	$('.show_img').click(function(){
		return false;
	})
	
})

function sales_by_sku(key){
	var arr=['1days','2days','3days','5days','7days','30days'];
	var days = arr[key];
	if(days == '1days'){
		var msg = '正在统计昨日销量...';
	}
	if(days == '2days'){
		var msg = '正在统计2天销量...';
	}
	if(days == '3days'){
		var msg = '正在统计3天销量...';
	}
	if(days == '5days'){
		var msg = '正在统计5天销量...';
	}
	if(days == '7days'){
		var msg = '正在统计7天销量...';
	}
	if(days == '30days'){
		var msg = '正在统计30天销量...';
	}
	layer.msg(msg, {icon:16, time:0, shade:0.3});
	$.ajax({
		type: 'get',
		url: '<?php echo U('api/jst_timer/sales_by_sku');?>', 
		data: {days:days},
		dataType: "json", 
		success: function (res) {
			layer.closeAll();
			if(res.status){
				layer.msg(res.message, {icon:1, time:2000},function(){
					//授权成功后刷新页面
					key++;
					if(key < arr.length){
						sales_by_sku(key);
					}else{
						location.href = location.href;
					}
				}); 
			}else{
				layer.msg(res.message, {icon:2});
			}
		}
	})
}

//点击导出新窗口打开
function openNewWindow(url) {
    var a = document.createElement('a');
    a.setAttribute('href', url);
    a.setAttribute('target', '_blank');
    var id = Math.random(10000, 99999);
    a.setAttribute('id', id);
    // 防止反复添加
    if (!document.getElementById(id)) {
        document.body.appendChild(a);
    }
    a.click();
}
</script>

</body>
</html>